// 添加新患者页面
var qcloud = require('../../../vendor/wafer2-client-sdk/index')
var config = require('../../../config')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'',
    userID:'',
    birthday:'',
    lastDate:'',
    pat_obj:{
      name:'患者姓名',
      nameFcous:false,
      sex:'',
      birthday:'',
      address:'家庭地址',
      addressFcous:false,
      relation:'患者和您的关系，如:朋友'
    },


      userInfo: {},
      takeSession: false,
      requestResult: '',
      addressf:false,
      inputerr:false,
      errmsg:'',
      patlist:''
  
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var today=util.formatTime(new Date());  
      console.log('页面传递参数',options);
       this.setData({
    //     username:options.name,
        birthday:today,
        lastDate:today
      

     })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },

  newdata:function(e){
    var tmpbirday="pat_obj.birthday";
    this.setData({
      birthday:e.detail.value,
      [tmpbirday]:e.detail.value
    })

  },



// 点击清除姓名内容
clearname:function (e) {
  var tmpname="pat_obj.name"
  if (e.detail.value == "患者姓名") {
     this.setData({
     [tmpname]:"",
  }) 
  }
},


// 用户名无内容检测
  ceckValue:function(e){
    var inValue=e.detail.value;
    var that = this;
    var tmpName="pat_obj.name";
    var tmpFoucs="pat_obj.nameFcous";
    if (inValue=='' || inValue =='患者姓名') {
      util.showModel('输入错误','姓名不能为空')
      
      that.setData({
        [tmpName]:"患者姓名",
        // [tmpFoucs]:true

      })

    }
  },


clearrelation:function (e) {
  var tmprelat="pat_obj.relation"
  if (e.detail.value == "患者和您的关系，如:朋友") {
    this.setData({
      [tmprelat]:""
    })
  }
  
},

ceckrelat:function (e) {
  var tmprel='pat_obj.relation';
  var tmpstr=e.detail.value;
 console.log('关系输入：',tmpstr.trim())
  if(tmpstr.trim()==''){
    this.setData({
      [tmprel]:"患者和您的关系，如:朋友"
    })
  }
  // body...
},


// 点击清除默认地址
clearaddress:function (e) {
  console.log(e);
  var tmpaddress="pat_obj.address"
  if (e.detail.value=="家庭地址") {
     this.setData({
     [tmpaddress]:"",
  }) 
  }
},

//检测患者地址是否为空
ceckaddress:function (e) {
  var tmpaddress=e.detail.value;
  var tmpd="pat_obj.address";
  if (tmpaddress.trim()=="") {
    this.setData({
      [tmpd]:"家庭地址"
    })
  }
},




// 下一步被单击测内容填写
choosepat:function (e) {
  
  // 地埴填写是否完整
  var that=this;
  this.data.errmsg='';
  if (e.detail.value.address=='家庭地址' || e.detail.value.address=='') {
    var addressFcous="pat_obj.addressFcous";   
    this.setData({
      // [addressFcous]:true,
      inputerr:true,
      errmsg:this.data.errmsg+"家庭地址不能为空!\n"      
    })
    };
 

  // 姓名是否填写
  if (e.detail.value.name=='您的姓名' || e.detail.value.name=='') {
    var nameFcous="pat_obj.nameFcous";
    this.setData({
      inputerr:true,
      // [nameFcous]:true,
      errmsg:this.data.errmsg+"姓名不能为空！ \n"
    })
  }; 

  // 出生日期是否进行了选择
  if (e.detail.value.birthday=='') {
    this.setData({
      inputerr:true,
      errmsg:this.data.errmsg+"请选择出生日期! \n"
    })
  }
  else{
    this.setData({
      inputerr:false
    })
  };

  if (this.data.inputerr) {
    return;
  } else {
    
// 获取患者列表
  wx.getStorage({
    key:"patlist",
    success:function (res) {
    that.setData({
      patlist:res.data
    })
   



   // 本次患者信息整理,根据取得的用户信息ID和openid换取用户列表，未完待续。
    var pat1={"birthday":e.detail.value.birthday,"from_op":getApp().globalData.openid,"from_id":getApp().globalData.userid,"pat_name":e.detail.value.name,"relation":e.detail.value.relation,"sex":e.detail.value.sex,'address':e.detail.value.address};
    

                 wx.request({
                   login:true,
                   url:'https://evekykeb.qcloud.la/weapp/SoboDb/inserpat',
                   method:'POST',
                   header: {'content-type':'application/x-www-form-urlencoded'},
                   data:{                     
                    // pat1
                     "birthday":e.detail.value.birthday,
                     "from_op":getApp().globalData.openid,
                     "from_id":getApp().globalData.userid,
                     "pat_name":e.detail.value.name,
                     "relation":e.detail.value.relation,
                     "sex":e.detail.value.sex,
                     "address":e.detail.value.address                   
                   },

                   success:function (res1) {
                     console.log(res1);
                     //  wx.navigateTo({
                     //   url:'../free2/free2?oppenid='+res.data[0].oppenid +'&userid='+res.data[0].user_id,
                     //   success:function (reg1) {
                     //     console.log(reg1);
                     //   },
                     //   fail:function (err1) {
                     //     console.log(err1);
                     //   }
                     // })
                   },

                   fail:function (err) {
                     // body...
                   },

             })








     if (pat1.sex=='男') {pat1.sex=1} else {pat1.sex=0}
    that.data.patlist[that.data.patlist.length]=pat1;
  
    that.setData({
      patlist:that.data.patlist
    })
   
   wx.setStorage({
    key:"patlist",
    data:that.data.patlist
   })

   console.log(that.patinser(that.data.patlist));
      
    wx.redirectTo({
     url: '../../freereg/free2/free2'
    })
 
  }
})

  }




},




















// 提交按钮后收集form表单数据。
  submitinfo:function(e){
    // 检验验证码是否正确，如果正确，将当前用户信息赋值给全局变量并向服务器提交信息
    // getApp().globalData.guser_obj=null;
    //console.log("表单数据",e);
    var tmpName="user_obj.name";
    var tmpPhnum="user_obj.phoneNum";
    var tmpsex="user_obj.sex";
    var reged="user_obj.reged";
    var address="user_obj.address";
    var birthday="user_obj.birthday"
    var that = this;

    // 将当前用户赋值给全局变量用户模型
    //console.log(getApp().globalData.guser_obj);
    that.setData({
      [tmpName]:e.detail.value.name,
      [tmpPhnum]:e.detail.value.phoneNum,
      [tmpsex]:e.detail.value.sex,
      [reged]:true,
      [address]:e.detail.value.address,
      [birthday]:e.detail.value.birthday


    })
    getApp().globalData.guser_obj=that.data.user_obj;
    var userobj=that.data.user_obj;
    
   // 写入用户信息表
    this.inser(userobj)

   },


      inser:function (e) {
            console.log('当前数据',e);            
             var open = getApp().globalData.openid;
             qcloud.request({
               login:true,
               url:'https://evekykeb.qcloud.la/weapp/SoboDb/inser',
               method:'POST',
               header: {'content-type':'application/x-www-form-urlencoded'},
               data:{
                 sex:e.sex,
                 address:e.address,
                 birthday:e.birthday,
                 phoneNum:e.phoneNum,
                 oppenid:open,
                 nicname:e.name,
               },
               success:function (res) {
                 //console.log("用户信息登记信息成功",res.data[0].user_id);
                 qcloud.request({
                   login:true,
                   url:'https://evekykeb.qcloud.la/weapp/SoboDb/inserpat',
                   method:'POST',
                   header: {'content-type':'application/x-www-form-urlencoded'},
                   data:{
                     
                     'pat_name':res.data[0].nicname,
                     'relation': '本人',
                     'birthday':res.data[0].birthday,
                     'sex':res.data[0].sex,
                     'from_id':res.data[0].user_id,
                     'from_op':res.data[0].oppenid
                   },

                   success:function (res1) {
                      wx.navigateTo({
                       url:'../free2/free2?oppenid='+res.data[0].oppenid +'&userid='+res.data[0].user_id,
                       success:function (reg1) {
                         console.log(reg1);
                       },
                       fail:function (err1) {
                         console.log(err1);
                       }
                     })
                   },

                   fail:function (err) {
                     // body...
                   },

             })
              },
              
               fail:function (error) {
                 console.log("登记信息错误",error);
               }

             })
          },




// 写入患者列表
     patinser:function (e) {
            console.log('当前数据',e);            
             var open = getApp().globalData.openid;
             qcloud.request({
               login:true,
               url:'https://evekykeb.qcloud.la/weapp/SoboDb/inserpat',
               method:'POST',
               header: {'content-type':'application/x-www-form-urlencoded'},
               data:{
                 pat_name:e.name,
                 sex:e.sex,
                 address:e.address,
                 birthday:e.birthday,
                 relation:e.relation,
                 from_id:e.from_id,
                 from_op:e.from_op
                 
               },
               success:function (res) {
                console.log(res);

              },
              
               fail:function (error) {
                 console.log("登记患者信息错误",error);
               }

             })
          },




})